const Service = require('egg').Service

class NoticeService extends Service {
    async emit (userId) {
        const { app,ctx } = this
        const id = await app.redis.get(userId)
        if (!id) return

        const docs = await ctx.model.Notice.find({ user_id:userId,see:false },{ type:1 })

        let full = {
            dynamic:0,
            news:0,
            agree:0,
            attention:0
        }

        for (let value of docs) {
            if (value.type === '动态') full.dynamic += 1
            if (value.type === '消息') full.news += 1
            if (value.type === '赞同') full.agree += 1
            if (value.type === '关注') full.attention += 1
        }


        app.io.of('/').to(id).emit('NOTICE',{ unread:docs.length,full })
    }
}

module.exports = NoticeService
